﻿@model SCM.Model.DBEntity.T_Material_Homogeneous
@{
    Layout = null;
}

<div class="home-desktop">
    <div class="content-pages-title">
        <h1>添加材质</h1>
    </div>
    <div class="content-notice-body">
        <!--基本信息-->
        <div class="archives-modular" style="display: block;">
            <form action="@(Url.Action("Save"))" method="post" id="form-add-materialHomogeneous">
                <table style="width: 100%; border: none;">
                    <tr>
                        <td valign="top">
                            <div class="notice-box-left">
                                <div class="modular-info" style="display: block;">
                                    <ul class="form">
                                        <li>
                                            <em>材质分类</em>
                                            @Html.DropDownList("MaterialCatalogID", ViewBag.MaterialCatalogList as IEnumerable<SelectListItem>, new { @id = "drop_materialCatalog" })
                                        </li>
                                        <li>
                                            <em>材质名称</em>
                                            <input type="text" name="name" maxlength="100" required />
                                        </li>
                                        <li class="row-last">
                                            <em>&nbsp;</em>&nbsp;&nbsp;
                                            <button type="button" class="cotrlBtn btnBlue" id="btn_save">保存</button>
                                        </li>
                                    </ul>
                                    <input type="hidden" id="hdMainId" name="id" value="" />
                                </div>
                            </div>
                        </td>
                    </tr>
                </table>
            </form>
        </div>

        <!--明细列表-->
        <div class="archives-modular" id="dtlList" data-tmp-id="d" style="margin-top:8px;">
            <form id="form-dtlList" action="@Url.Action("SaveDtlList")" method="post" enctype="multipart/form-data">
                <div class="modular-title"><h2>成分列表</h2></div>
                <div class="modular-info" style="display: block;">
                    <div style="padding-right: 10px;">
                        <table class="job-item-table" id="tb_dtlList">
                            <thead>
                                <tr>
                                    <th>成分名称</th>
                                    <th>CAS代码</th>
                                    <th>含量百分比</th>
                                    <th>操作</th>
                                </tr>
                            </thead>
                            <tbody>
                                <tr class="last">
                                    <td colspan="5" style="text-align: center;">
                                        <button type="button" class="cotrlBtn btnDefault left add-table-item"><i class="icon-add"></i>添加</button>
                                        <button type="button" class="cotrlBtn btnBlue" id="btnSaveDtl">保存</button>
                                    </td>
                                </tr>
                            </tbody>
                        </table>
                    </div>
                </div>
            </form>
        </div>
    </div>
</div>

<script type="text/html" id="tmp-d">
    <!--模版d-->
    <tr>
        <td style="min-width:150px;"><input type="hidden" class="zyclass" name="id" /><input type="text" name="cname" placeholder="请选择成分"  readonly="readonly" class="table-item-ctrol-btn"></td>
        <td><input type="text" name="casNo" readonly="readonly" class="input-title"></td>
        <td><input type="number" name="concentration" maxlengths="28" class="input-title"></td>
        <td><a href="javascript:void(0);" onclick="$(this).closest('tr').remove();" title="删除">删除</a></td>
    </tr>
</script>
<script type="text/javascript">
    //保存基本信息
    $("#btn_save").click(function () {
        if ($('#form-add-materialHomogeneous').valid()) {
            $("#form-add-materialHomogeneous").ajaxSubmit({
                datatype: "json",
                data: $('#form-add-materialHomogeneous').serialize(),
                success: function (result) {
                    if (result.code == 0) {
                        $("#hdMainId").val(result.data);
                        dcAlert(result.message, 0, 1, function () {
                            //$(".layer-dialog-close-btn").click();
                            //刷新显示数据
                            btn_search_click();
                        });
                    }
                    else {
                        dcAlert(result.message, 0, 2);
                    }
                }
            });
        }
    });

    //保存明细
    $("#btnSaveDtl").click(function () {
        var mainId = $("#hdMainId").val();
        if (mainId == null || mainId == "") {
            dcAlert("请添加基础信息", 0, 2);
            return;
        }

        var errorMsg = "";
        var dtlList = new Array();

        //校验数据，生成提交数据集合
        $("#tb_dtlList >tbody>tr:not(.last)").each(function () {
            var tdList = $(this).children();

            var id = tdList.eq(0).find('input[type="hidden"]').val();
            var cname = tdList.eq(0).find('input[type="text"]').val();
            var casNo = tdList.eq(1).find('input[type="text"]').val();
            var concentration = tdList.eq(2).find('input[type="number"]').val();

            if (cname == null || cname == "") {
                errorMsg = "请选择成分";
                tdList.eq(0).find('input[type="text"]').focus();
                return false;
            }
            if (casNo == null || casNo == "") {
                errorMsg = "请填写含量";
                tdList.eq(1).find("input").focus();
                return false;
            }

            dtlList.push({
                id: id,
                materialHomogeneousID: mainId,
                materialIngredientID: casNo,
                concentration, concentration
            });
        });

        if (errorMsg != "") {
            dcAlert(errorMsg, 0, 2);
            return;
        }

        if ($("#form-dtlList").valid()) {
            $("#form-dtlList").ajaxSubmit({
                datatype: "json",
                data: { mainId: mainId, dtlList: dtlList },
                success: function (result) {
                    if (result.code == 0) {
                        dcAlert(result.message, 0, 1, function () {
                            if (result.data != "") {
                                //添加后让页面name为id的隐藏域有值
                                var idList = result.data.split(",");
                                $(".zyclass").each(function (index, element) {
                                    $(this).val(idList[index]);
                                });
                            }
                            //刷新显示数据
                            btn_search_click();
                        });
                    }
                    else {
                        dcAlert(result.message, 0, 2);
                    }
                }
            });
        }
    });
</script>
<script type="text/javascript">
    $('#tb_dtlList').on("click", '.table-item-ctrol-btn', function () {
        var tdList = $(this).parent().parent().children();
        layer.open({
            type: 2,
            title: "选择成分",
            shadeClose: true,
            shade: 0.4,
            area: ['80%', '90%'],
            offset: ['5%', '10%'],
            content: "@Url.Action("SelectView", "BasicIngredient")",
            btn: ['确定', '关闭'],
            yes: function (index) {
                //当点击‘确定’按钮的时候，获取弹出层返回的值
                var selectRows = window["layui-layer-iframe" + index].callbackdata();
                //打印返回的值，看是否有我们想返回的值。
                if (selectRows == "" || selectRows.length != 1) {
                    dcAlert("请选择一条数据", 0, 2);
                    return false;
                }
                var dataArray = selectRows[0].split('|');
                tdList.eq(0).find('input[name="cname"]').val(dataArray[1]);
                tdList.eq(1).find('input[name="casNo"]').val(dataArray[0]);

                //最后关闭弹出层
                layer.close(index);
            },
            cancel: function () {
                //右上角关闭回调
            }
        });
    });
</script>

